package pure.ui
{
	import pure.ui.core.LabelComponent;
	import pure.ui.groups.ButtonBar;
	import pure.ui.groups.GroupBase;
	import pure.ui.skins.ClickableSkin;
	import pure.ui.skins.IClickSkin;
	import pure.ui.skins.ISkin;
	import pure.ui.skins.SkinColor;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.utils.*;

	/**
	 * 按钮
	 * @author Des
	 * <皮肤种类> ---- IClickSkin
	 * 
	 * <TODO> 支持按下连续触发功能，间隔最好不低于0.1秒！
	 */

public class Button extends LabelComponent
{
	public function Button(x:Number = 0, y:Number = 0, size:Point = null , skin:ISkin = null, hasLabel:Boolean = true)
	{
		super(x, y, size,skin, hasLabel );
	}

	
	
	/*default*/
	
	protected static const INTERVAL:Number = 200;	// 默认按下连续触发间隔(ms)
	
	/*model*/

	protected var _isOver:Boolean;	// 是否滑过
	
	protected var _isDown:Boolean;	// 是否按下
	
	/*cache*/

	protected var _addUpT:int;
	
	protected var _oldT:int;
	
	protected var _interval:Number;	// 按下连续触发间隔(ms)
	
	protected var ____clickObserver:Function;	// 点击回调方法
	
	protected var ____buttonDownObserver:Function;	// 按下连续触发回调方法

	
	
	
	/**
	 * 观察点击状态
	 * @param observer		回调方法
	 **/
	public function setObserverClick(observer:Function):void
	{
		if (observer != null)
		{ 
			this.addEventListener(MouseEvent.CLICK, ____onClick);
		}
		else
		{
			this.removeEventListener(MouseEvent.CLICK, ____onClick);
		}
		____clickObserver = observer;
	}
	
	/**
	 * 按住连续触发回调
	 * @param observer			回调方法
	 **/
	public function setObserverButtonDown(observer:Function):void
	{
		if (observer != null)
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN, ____onPressed);
		}
		else {
			this.removeEventListener(MouseEvent.MOUSE_DOWN, ____onPressed);
			if (isPressed) 
			{
				____onButtonUp();
			}
		}
		____buttonDownObserver = observer;
	}
	
	/**
	 * 毁灭
	 */
	override public function destroy():void
	{
		super.destroy();
		this.removeEventListener(MouseEvent.MOUSE_DOWN, ____onMouseDown);
		this.removeEventListener(MouseEvent.ROLL_OVER, ____onMouseOver);
		if (this._isDown) 
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, ____onMouseUp);
		}
		if (this._isOver) 
		{
			this.removeEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);
		}
		this.____clickObserver = null;
		this.____buttonDownObserver = null;
	}
	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	// 按钮皮肤
	override public function set skin(value:ISkin):void
	{
		if (!value is IClickSkin)
		{
			throw new Error('Button::skin - 皮肤类型错误！');
		}
		super.skin = value;
	}

	// 按钮按住事件派发间隔(秒)
	public function set interval(value:Number):void
	{
		this._interval = value * 1000;
	}
	
	// 获取是否正在被按下
	public function get isPressed():Boolean
	{
		return this._isDown;
	}
	
	// 所在group
	override public function set group(value:GroupBase):void
	{
		if (!value is ButtonBar)
		{
			throw new Error('ButtonBar::group - 组合类型错误！');
		}
		_group = value;
	}
	
	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	
	
	override protected function _initSelf():void
	{
		super._initSelf();
		this.mouseChildren = false;
		this._isDown = false;
		this._isOver = false;
		this._interval = INTERVAL;
		this._padding = 0;
		this.addEventListener(MouseEvent.MOUSE_DOWN, ____onMouseDown);
		this.addEventListener(MouseEvent.ROLL_OVER, ____onMouseOver);
	}

	
	/**
	 * 点击
	 */
	protected function ____onClick(e:MouseEvent):void
	{
		if (____clickObserver != null)
		{
			____clickObserver();
		}
	}
	
	/**
	 * 按下
	 */
	protected function ____onMouseDown(e:MouseEvent):void
	{
		stage.addEventListener(MouseEvent.MOUSE_UP, ____onMouseUp);
		this._isDown = true;	
		if (_group)
		{
			_group.selectedId = this._id;
		}
		update();
	}
	
	/**
	 * 抬起
	 */
	protected function ____onMouseUp(e:MouseEvent):void
	{
		stage.removeEventListener(MouseEvent.MOUSE_UP, ____onMouseUp);
		this._isDown = false;
		update();
	}
	
	/**
	 * 滑过
	 */
	protected function ____onMouseOver(e:MouseEvent):void
	{
		this._isOver = true;
		this.addEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);	
		update();
	}
	
	/**
	 * 滑鼠离开
	 */
	protected function ____onMouseLeave(e:MouseEvent):void
	{
		this.removeEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);
		this._isOver = false;
		update();
	}

	/**
	 * 绘制
	 */
	override protected function _draw():void
	{
		// 按下
		if (this._isDown)
		{
			if (this._skin)
			{
				if (_getBitmap("down"))
				{
					_skip("down");
					_clear();
				}
				else 
				{
					if (this._bitmap.bitmapData)
					{
						this._bitmap.bitmapData = null;
					}
					_definition = null;
					_drawNoSkin(SkinColor.BTN_DOWN,1,4);
				}
			}
			else 
			{
				_drawNoSkin(SkinColor.BTN_DOWN,1,4);
			}
			return;
		}
		
		// 滑过
		if (this._isOver)
		{
			if (this._skin)
			{
				if (_getBitmap("over")) 
				{
					_skip("over");
					_clear();
				}
				else 
				{
					if (this._bitmap.bitmapData) 
					{
						this._bitmap.bitmapData = null;
					}
					_definition = null;
					_drawNoSkin(SkinColor.BTN_OVER,1,4);
				}
			}
			else 
			{
				_drawNoSkin(SkinColor.BTN_OVER,1,4);
			}
			return;
		}
		
		// 移出
		if (this._skin)
		{
			if (_getBitmap("out")) 
			{
				_skip("out");
				_clear();
			}
			else
			{
				if (this._bitmap.bitmapData) 
				{
					this._bitmap.bitmapData = null;
				}
				_definition = null;
				_drawNoSkin(SkinColor.BTN_OUT,1,4);
			}
		}
		else 
		{
			_drawNoSkin(SkinColor.BTN_OUT,1,4);
		}
		
		super._draw();
	}
	
	/**
	 * 观察按住状态
	 */
	protected function ____onPressed(e:MouseEvent):void
	{
		this._addUpT = 0;
		this._oldT = getTimer();
		if (____buttonDownObserver != null)
		{
			____buttonDownObserver();
		}
		addEventListener(Event.ENTER_FRAME, ____checkButtonDown);
		stage.addEventListener(MouseEvent.MOUSE_UP, ____onButtonUp);
	}
	
	/**
	 * 逐帧观察按住状态
	 */
	protected function ____checkButtonDown(e:Event):void
	{
		var elapseT:int = getTimer() - this._oldT;
		this._addUpT += elapseT;
		if (this._addUpT >= this._interval)
		{
			if (____buttonDownObserver != null)
			{
				____buttonDownObserver();
			}
			this._addUpT %= this._interval;
		}	
		this._oldT = getTimer();
	}
	
	/**
	 * 观察按住状态结束
	 */
	protected function ____onButtonUp(e:MouseEvent = null):void
	{
		removeEventListener(Event.ENTER_FRAME, ____checkButtonDown);
		stage.removeEventListener(MouseEvent.MOUSE_UP, ____onButtonUp);
		this._addUpT = 0;
		this._oldT = 0;
	}
		
	/**
	 * 调整位置
	 **/
	//override protected function _rejust():void
	//{
		//this._textField.x = -this._textField.width * 0.5 + this._padding;
		//this._textField.y = - this._textField.height >> 1;
	//}
}
}